/**
 * 
 */
package com.innova_t.android.mario;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.innova_t.android.mario.items.ContactDTO;


/**
 * Customization of the basic list to display the contact object
 * 
 * @author mario
 *
 */
public class ContactAdapter extends BaseAdapter {
	
	/** main source of data */
	private List<ContactDTO> listoListas = null; 
	
	public ContactAdapter(){
		//empty constructor
	}
	
	public ContactAdapter(List<ContactDTO> listas){
		listoListas = listas;
	}
	

	@Override
	public int getCount() {
		return listoListas.size();
	}

	@Override
	public Object getItem(int index) {
		return listoListas.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		
		ViewGroup item = getViewGroup2(convertView, parent); 
		 
		TextView counter = (TextView)item.findViewById(R.id.counter); 
		TextView label = (TextView)item.findViewById(R.id.text);
		
		// now obtain the object
		ContactDTO listoListaLocal = null; // listoListas.get(pos);
		
		// TODO: cambiar a un public attribute
//		counter.setText( Long.toString(listoListaLocal.getId() ) );
		label.setText(listoListaLocal.name );
		
		// fancy for displaying only the ones
		
		return item; 
	}
	
	/**
	 * Private helper function from previous sdk
	 * 
	 * @param convertView
	 * @param parent
	 * @return
	 */
	private ViewGroup getViewGroup2(View convertView, ViewGroup parent){
		
		if( convertView instanceof ViewGroup ){
			return (ViewGroup) convertView;
		}
		Context context = parent.getContext(); 
	    LayoutInflater inflater = LayoutInflater.from(context); 
	    ViewGroup item = (ViewGroup)inflater.inflate( 
	            R.layout.contact_enhanced_item, null);
	 
	    return item;
	}

}
